package com.yykj.user.center.server.controller;

import com.yykj.common.vo.ResultVO;
import com.yykj.user.center.client.form.AddGroupForm;
import com.yykj.user.center.server.form.AddRelationGroupPatient;
import com.yykj.user.center.server.form.RemoveGroupForm;
import com.yykj.user.center.server.form.RemovePatientForm;
import com.yykj.user.center.server.form.UpdateGroupForm;
import com.yykj.user.center.server.service.GroupService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * Created by chendg
 * on 2021/9/10 0010.
 */
@Api(tags = "分组或标签接口")
@RestController
@RequestMapping("/api/group")
@SuppressWarnings("unchecked")
@Slf4j
public class GroupController {
    @Autowired
    private GroupService groupService;

    @PostMapping("/create")
    @ApiOperation("创建分组或标签 ")
    public ResultVO create(@Valid @RequestBody AddGroupForm addGroupForm){
        return ResultVO.success(groupService.create(addGroupForm));
    }

    @PostMapping("/createDefault")
    @ApiOperation("创建默认分组 ")
    public void createDefault(@Valid @RequestBody AddGroupForm addGroupForm){
         groupService.createDefault(addGroupForm);
    }

    @PostMapping("/update")
    @ApiOperation("编辑分组 ")
    public ResultVO update(@Valid @RequestBody UpdateGroupForm updateGroupForm){
        groupService.update(updateGroupForm);
        return ResultVO.success();
    }

    @PostMapping("/buildRelation")
    @ApiOperation("将患者与标签或分组建立关系或者移至分组")
    public ResultVO buildRelation(@Valid @RequestBody AddRelationGroupPatient addRelationGroupPatient){
        return ResultVO.success(groupService.buildRelation(addRelationGroupPatient));
    }

    @PostMapping("/removePatientFromGroup")
    @ApiOperation("从分组或标签中删除患者 ")
    public ResultVO removePatientFromGroup(@Valid @RequestBody RemovePatientForm removePatientForm){
        return ResultVO.success(groupService.removePatientFromGroup(removePatientForm));
    }


    @ApiImplicitParams({
            @ApiImplicitParam(name = "hospitalId", value = "医院ID", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "doctorId", value = "医生ID", required = true, dataType = "String"),
            @ApiImplicitParam(name = "type", value = "类型1：分组 2：标签", required = true, dataType = "Integer"),
    })
    @GetMapping("/queryByGroup")
    @ApiOperation("根据分组或标签来展示患者")
    public ResultVO queryByGroup(@RequestParam(value = "hospitalId",required = true)Integer hospitalId,
                                 @RequestParam(value="doctorId",required = true)String doctorId,
                                 @RequestParam(value = "type",required = true)Integer type){
        return ResultVO.success(groupService.queryByGroup(hospitalId,doctorId,type));
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "doctorId", value = "医生ID", required = true, dataType = "String"),
    })
    @GetMapping("/queryAllPatientByDoctorId")
    @ApiOperation("根据医生获取所有的患者信息")
    public ResultVO queryAllPatientByDoctorId(@RequestParam(value="doctorId",required = true)String doctorId){
        return ResultVO.success(groupService.queryAllPatientByDoctorId(doctorId));
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "doctorId", value = "医生ID", required = true, dataType = "String"),
            @ApiImplicitParam(name = "hospitalId", value = "医院ID", required = true, dataType = "Integer"),
    })
    @GetMapping("/availableGroupPatients")
    @ApiOperation("获取分组中可勾选患者")
    public ResultVO availableGroupPatients(@RequestParam(value="doctorId",required = true)String doctorId,@RequestParam(value="hospitalId",required = true)Integer hospitalId){
        return ResultVO.success(groupService.availableGroupPatients(doctorId,hospitalId));
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "doctorId", value = "医生ID", required = true, dataType = "String"),
            @ApiImplicitParam(name = "hospitalId", value = "医院ID", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "labelId", value = "标签ID", required = true, dataType = "String"),
    })
    @GetMapping("/availableLabelPatients")
    @ApiOperation("获取标签中可勾选患者")
    public ResultVO availableLabelPatients(@RequestParam(value="doctorId",required = true)String doctorId,@RequestParam(value="hospitalId",required = true)Integer hospitalId,@RequestParam(value="labelId",required = true)String labelId){
        return ResultVO.success(groupService.availableLabelPatients(doctorId,hospitalId,labelId));
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "doctorId", value = "医生ID", required = true, dataType = "String"),
            @ApiImplicitParam(name = "hospitalId", value = "医院ID", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "type", value = "类型1：患者 2：标签", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "name", value = "患者名称或标签名", required = false, dataType = "String"),
    })
    @GetMapping("/list")
    @ApiOperation("根据患者或者标签进行查询")
    public ResultVO list(@RequestParam(value="doctorId",required = true)String doctorId,
                         @RequestParam(value="hospitalId",required = true)Integer hospitalId,
                         @RequestParam(value="type",required = true)Integer type,
                         @RequestParam(value="name",required = false)String name){
        return ResultVO.success(groupService.list(doctorId,hospitalId,type,name));
    }

    @PostMapping("/removeGroup")
    @ApiOperation("删除分组或标签 ")
    public ResultVO removeGroup(@Valid @RequestBody RemoveGroupForm removeGroupForm){
        return ResultVO.success(groupService.removeGroup(removeGroupForm));
    }
}
